package cn.edu.bupt.blog.web;

import cn.edu.bupt.blog.po.Tag;
import cn.edu.bupt.blog.service.BlogService;
import cn.edu.bupt.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TagShowController {
    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags")
    public String types(@PageableDefault(size = 1, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @RequestParam Long id, Model model) {
        // 查询所有分类,使用10000来作为查询条数，肯定包含所有分类
        List<Tag> tags = tagService.listTag(10000);
        // 如果传递过来的id为-1，代表通过标题栏点击进入，展示第一条分类对应的博客
        if(id == -1) {
            id = tags.get(0).getId();
        }
        model.addAttribute("tags",tags);
        model.addAttribute("activeTagId",id);
        model.addAttribute("page",blogService.listBlog(id,pageable));
        return "tags";
    }
}
